package fms.core;

import fms.acarsMsgProcessUtil.DownLinkDatagramHeader;
import fms.acarsMsgProcessUtil.RawDatagram;
import fms.util.ConfUtil;
import fms.util.ObjectUtil;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WindMsgFilter implements IFilter {
    private Logger logger = Logger.getLogger(WindMsgFilter.class);

    // 风温下行请求报文的SMI列表
    private List downQeqList = new ArrayList();

    public WindMsgFilter() {
        File fBaseParam = new File(ConfUtil.props.getProperty("baseParamFilePath"));
        final SAXReader readerBaseParam = new SAXReader();
        try {
            final Document paraseRulenDoc = readerBaseParam.read(fBaseParam);
            Element uniqueObject = (Element) paraseRulenDoc.selectSingleNode("/baseParamter/coreService/windController/DownQeqSMI");
            String[] downQeqSMI = uniqueObject.getText().split(",");
            for (int i = 0; i < downQeqSMI.length; i++) {
//                logger.debug("WindFilter构造，发现SMI：" + downQeqSMI[i]);
                downQeqList.add(downQeqSMI[i]);
            }
        } catch (final DocumentException e) {
            logger.error(e.getMessage());
        }
    }

    // 判断当前报文是否是风温请求报文
    public boolean isFilter(String aContent) {
//        logger.debug("[风温请求报文过滤] 开始进行报文过滤...");
        if (aContent.contains("<?xml") && aContent.contains("<request>")) {
            return false;
        } else if (aContent.contains("<?xml") && aContent.contains("FMS")) {
            return false;
        } else{
            DownLinkDatagramHeader msgHeader = null;
            String msgText = "";
            try {
                msgHeader = DownLinkDatagramHeader.Parse(RawDatagram.GetHeader(aContent));
                msgText = RawDatagram.GetBody(aContent);
            } catch (Exception e) {
                logger.error("报文头解析出错！" + e.getMessage() + "\r\n出错报文为：\r\n" + aContent);
                // 报头解析异常，该报文不是正规的acars报文，可略过
                return false;
            }

            if (!ObjectUtil.isBlank(msgHeader.getSmi()) && (downQeqList.indexOf(msgHeader.getSmi()) >= 0) &&
                    !ObjectUtil.isBlank(msgText) && msgText.substring(0, 6).equals("REQPWI")) {
                logger.debug("[风温请求报文过滤] 该报文是风温请求报！");
                /**
                 * 下面逻辑判断作用：仅对配置过的航空公司风温请求进行响应
                 * */
                String componystr = ConfUtil.props.getProperty("USEAIRCOMPANY");
                String componys[] = null;
                if(componystr != null) {
                    componys = componystr.split("\\,");
                }
                if (componys != null && !(ConfUtil.IsCanCompony(msgHeader.getFi().getAgencyID(), componys))) {
//                    logger.debug("[风温请求报文过滤] 该航空公司" + msgHeader.getFi().getAgencyID() + "请求报文不是配置的航空公司，不予处理！");
                    return false;
                }
                return true;
            } else if (!ObjectUtil.isBlank(msgHeader.getSmi()) && (downQeqList.indexOf(msgHeader.getSmi()) >= 0) &&
                    !ObjectUtil.isBlank(msgText) && msgText.substring(0, 6).equals("REJPWI")) {
                logger.debug("[风温请求报文过滤] 发现‘FMS上传信息拒绝报文’：" + aContent);
                return  false;
            } else {
//                logger.debug("[风温请求报文过滤] 该报文不是风温请求报！");
//                    if (ObjectUtil.isBlank(msgHeader.getSmi())) {
//                        logger.debug("smi 为空");
//                    } else {
//                        logger.debug("msgHeader.getSmi():" + msgHeader.getSmi() + "---" );
//                    }
//                    if (ObjectUtil.isBlank(msgText)) {
//                        logger.debug("msgText 为空");
//                    } else {
//                        logger.debug("msgText.substring(0, 6):" + msgText.substring(0, 6) + "---" );
//                    }
                return false;
            }
        }
    }
//    public boolean isFilter(String aContent) {
//        logger.debug("[(MSGUPCENTER转发)风温回复报文过滤] 开始进行风温回复报文过滤...");
//        if (aContent.contains("<?xml") && aContent.contains("FMS")) {
//            logger.debug("[(MSGUPCENTER转发)风温回复报文过滤] 该报文是风温回复报文...");
//            return true;
//        } else {
//            logger.debug("[(MSGUPCENTER转发)风温回复报文过滤] 该报文不是风温回复报文...");
//            return false;
//        }
//	}


}